package docview;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.tree.DefaultMutableTreeNode;
/**
 *
 * @author gleb
 */
public class PriceSessionStore
{
    private List<PriceSessionStoreObject> storage = new ArrayList<PriceSessionStoreObject>();
    public boolean add(String _value, String _col_name,DefaultMutableTreeNode _node)
    {
        return storage.add(new PriceSessionStoreObject(_value,_col_name,_node));
    }
    public boolean add(String _value, String _col_name,int _row, int _col,DefaultMutableTreeNode _node)
    {
        return storage.add(new PriceSessionStoreObject(_value,_col_name,_row,_col,_node));
    }
    public boolean add(PriceSessionStoreObject obj)
    {
        return storage.add(obj);
    }
    public int getRowCountForCathegory(DefaultMutableTreeNode node)
    {
        int max = 0;
        for (int i = 0 ; i < storage.size(); i++)
        {
            if (node.equals(storage.get(i).getNode()))
            {
                if (storage.get(i).getRow() > max)
                {
                    max = storage.get(i).getRow();
                }
            }
        }
        return max;
    }
    public int getColCountForCathegory(DefaultMutableTreeNode node)
    {
        int max = 0;
        for (int i = 0 ; i < storage.size(); i++)
        {
            if (node.equals(storage.get(i).getNode()))
            {
                if (storage.get(i).getCol() > max)
                {
                    max = storage.get(i).getCol();
                }
            }
        }
        return max;
    }
    public void removeByColumnAndRow(DefaultMutableTreeNode node, int row, int col)
    {
        for (int i = 0 ; i < storage.size(); i++)
        {
            if (storage.get(i).getCol()==col && storage.get(i).getRow() == row && storage.get(i).getNode().equals(node))
            {
                storage.remove(i);
            }
        }
    }
    public void removeByRow(DefaultMutableTreeNode node, int row)
    {
        for (int i = 0 ; i < storage.size(); i++)
        {
            if (storage.get(i).getRow() == row && storage.get(i).getNode().equals(node))
            {
                storage.remove(i);
                
            }
        }
    }
    public Iterator<PriceSessionStoreObject> getContentByCathegory(DefaultMutableTreeNode node)
    {
        List<PriceSessionStoreObject> list = new ArrayList<PriceSessionStoreObject>();
        Iterator<PriceSessionStoreObject> iter = storage.iterator();
        while (iter.hasNext())
        {
            PriceSessionStoreObject obj = iter.next();
            if(obj.getNode().equals(node))
            {
                list.add(obj);
            }
        }
        return list.iterator();
    }
    public Iterator<DefaultMutableTreeNode> getCathegories()
    {
        Set<DefaultMutableTreeNode> set = new HashSet<DefaultMutableTreeNode>();
        for (int i = 0; i < storage.size(); i++ )
        {
            set.add(storage.get(i).getNode());
        }
        return set.iterator();
    }
    public PriceSessionStoreObject getByPosition(int row, int col,DefaultMutableTreeNode node)
    {
        for (int i = 0 ; i < storage.size(); i++)
        {
            if (    storage.get(i).getCol()== col
                    && storage.get(i).getRow() == row
                    && storage.get(i).getNode().equals(node))
            {
                return storage.get(i);
            }
        }
        return null;
    }
    public int getNumOfValues()
    {
        return storage.size();
    }
    public int getNumOfCathegories()
    {
        Set<DefaultMutableTreeNode> set = new HashSet<DefaultMutableTreeNode>();
        for (int i = 0; i < storage.size(); i++ )
        {
            set.add(storage.get(i).getNode());
        }
        return set.size();
    }
}
